<!-- 多个玩家调控 -->
<template>
  <el-dialog :modelValue="props.show" :title="t('779')" width="85%" top="4vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="handleClose">
    <div class="dialogMaxMultiple">
      <SearchTable :query="data.query" :list="data.list" :total="data.total" :showSet="false" :showExport="false" :showPagination="data.userControlList.length ? true : false"
        @changeSize="changeSize" @changePage="changePage" @search="getSearchList" @select="playerControl" @reset="onHideDetails">
        <template #form>
          <el-form-item label="">
            <el-select class="selectWidth120" v-model="data.query.search_type" :placeholder="t('340')">
              <el-option v-for="item in data.conditionList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
            <el-input class="width200" v-model="data.query.search_value" :placeholder="t('19')" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
          <el-form-item label="">
            <el-select class="selectWidth150" v-model="data.query.time_type" :placeholder="t('340')">
              <el-option :label="t('24')" :value="1" />
              <el-option :label="t('32')" :value="2" />
            </el-select>
            <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" :start-placeholder="t('26')"
              :end-placeholder="t('27')" value-format="YYYY-MM-DD" clearable :disabled-date="indexStore.disabledDateTime"/>
          </el-form-item>
          <el-form-item :label="t('780')+'：'">
            <el-input class="width200" v-model="data.query.today_bet" :placeholder="t('19')" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
          <el-form-item :label="t('485')+'：'">
            <el-input class="width200" v-model="data.query.total_bet" :placeholder="t('19')" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
          <el-form-item :label="t('377')+'：'">
            <el-input class="width200" v-model="data.query.today_game_income" :placeholder="t('19')" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
          <el-form-item :label="t('81')+'：'">
            <el-input class="width200" v-model="data.query.total_game_income" :placeholder="t('19')" clearable
              onKeyUp="value=value.replace(/[\W]/g,'')" />
          </el-form-item>
        </template>
        <template #button>
          <el-button type="warning" size="small" :disabled="data.userControlList.length ? false : true" @click="data.isControlShow = true">{{t('776')}}</el-button>
        </template>
        <template #table>
          <el-table-column type="selection" width="45" />
          <template v-for="(item, index) in data.fields" :key="index">
            <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width">
              <template #header>
                <el-tooltip v-if="item.tips" effect="dark" placement="top">
                  <template #content>
                    {{ item.tips }}
                  </template>
                  <el-icon class="ques-tips">
                    <QuestionFilled />
                  </el-icon>
                </el-tooltip>
                {{ item.label }}
              </template>
              <template v-if="item.prop == 'today_bet'" #default="scope">
                <span class="colorText"> {{ BigWan(scope.row.today_bet) }} </span>
              </template>
              <template v-if="item.prop == 'total_bet'" #default="scope">
                <span class="colorText"> {{ BigWan(scope.row.total_bet) }} </span>
              </template>
              <template v-if="item.prop == 'today_game_income'" #default="scope">
                <span :class="colorClass(scope.row.today_game_income)"> {{ BigWan(scope.row.today_game_income) }}
                </span>
              </template>
              <template v-if="item.prop == 'total_game_income'" #default="scope">
                <span :class="colorClass(scope.row.total_game_income)"> {{ BigWan(scope.row.total_game_income) }}
                </span>
              </template>
              <template v-if="item.prop == 'amount'" #default="scope">
                <span class="colorText"> {{ BigWan(scope.row.amount) }} </span>
              </template>
              <template v-if="item.prop == 'is_online'" #default="scope">
                <el-tag :type="scope.row.is_online ? 'success' : 'danger'">{{
                  selectToLabel(ONLINESTATE, scope.row.is_online) }}</el-tag>
              </template>
              <template v-if="item.prop == 'created_at_stamp'" #default="scope">
                {{ setTimestampTime(scope.row.created_at_stamp) }}
              </template>
              <template v-if="item.prop == 'last_login_stamp'" #default="scope">
                {{ setTimestampTime(scope.row.last_login_stamp) }}
              </template>
            </el-table-column>
          </template>
        </template>
      </SearchTable>
    </div>
  </el-dialog>

  <!-- 批量玩家调控 -->
  <el-dialog :modelValue="data.isControlShow" :title="t('781')" width="60%" top="20vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="data.isControlShow = false">
    <div class="dialogMaxHeight">
      <div class="padding10">
        <form-item :title="t('781')">
          <div class="flexWrap">
            <span class="margin5" v-for="(item, index) in data.userControlList" :key="index">
              【{{ item.account3d_id }}】{{ item.account3d }}
            </span>
          </div>
        </form-item>
        <form-item :title="t('768')" :pass="true" value="game_id">
          <el-select class="imWidth100" v-model="data.userControl.game_ids" :placeholder="t('340')" filterable multiple
            @change="chooseGameType">
            <el-option :label="t('21')" :value="-1" />
            <el-option v-for="(item, index) in indexStore.gameArray" :key="index"
              :label="'【' + item.value + '】' + GameName(indexStore.gameArray,item.value)" :value="item.value" v-show="item.type == 1 ? true : false" />
          </el-select>
        </form-item>
        <form-item :title="t('866')" value="rtp">
          <el-input-number v-model="data.userControl.auto_cancel_rtp" :min="0" :max="99999999999999999999" :precision="0"
            class="width150" :placeholder="t('19')" clearable :controls="false" />%
          <span class="tipsText">({{t('867')}})</span>
        </form-item>
        <form-item title="RTP" :pass="true" value="rtp">
          <el-select class="width150" v-model="data.userControl.rtp" :placeholder="t('340')" filterable>
            <el-option v-for="(item, index) in data.rtpList" :key="index" :label="item + '%'" :value="item" v-show="item==200?data.agentTypeShow:true"/>
          </el-select>
          <el-button class="marginL20" type="success" :disabled="data.userControl.game_ids.length ? false : true"
            @click="addControl">{{t('777')}}</el-button>
        </form-item>
      </div>
    </div>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, watch, ref } from 'vue'
import { ElMessage } from 'element-plus'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import FormItem from '@/components/FormItem/FormItem.vue'
import { throttle, selectToLabel, colorClass, setCommaToArr,GameName } from '@/utils/index'
import { setTimestampTime, setTimeValue, getSevenWeek } from '@/utils/time'
import { getUserList, changeAllControlUser } from '@/api/user'
import { getOperatorTiger } from '@/api/settion'
import { BigWan, BigTimeWan, BigHundred, BigTimeHundred } from '@/utils/math'
import { ONLINESTATE } from '@/config/eunm'

import { useStore } from '@/stores/index'
const indexStore = useStore()
import { useI18n } from 'vue-i18n'
const { t } = useI18n()

const props = defineProps({
  show: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  agentItem: {
    type: Object,
    default: () => {
      return {
        agent_id: 1,
        operated_id: 0,
      }
    }
  },
})


watch(
  () => props.show,
  async (val) => {
    if (val) {
      getGameRtp(0, 39)
      getSearchList()
    }
  }
)

const data = reactive({
  query: {
    page: 1,
    size: 50,
    status: -1,
    search_type: 1,
    search_value: '',
    time_type: 1,
    is_online: -1,
    today_bet: '',
    total_bet: '',
    today_game_income: '',
    total_game_income: '',
    date: getSevenWeek()
  },
  list: [],
  total: 0,
  fields: [
    { label: t('24'), prop: 'created_at_stamp', width: '180', visible: true, tips: t('299') },
    { label: t('32'), prop: 'last_login_stamp', width: '180', visible: true, tips: t('298') },
    { label: t('17'), prop: 'account3d_id', width: '150', visible: true, tips: t('289') },
    { label: t('18'), prop: 'account3d', width: '180', visible: true, tips: t('290') },
    { label: t('376'), prop: 'today_bet', width: '', visible: true, sortable:true, tips: t('535') },
    { label: t('485'), prop: 'total_bet', width: '', visible: true, tips: t('536') },
    { label: t('377'), prop: 'today_game_income', width: '', visible: true, sortable:true, tips: t('537') },
    { label: t('378'), prop: 'total_game_income', width: '', visible: true, sortable:true, tips: t('538') },
    { label: t('31'), prop: 'amount', width: '', visible: true, tips: t('296', { br: '<br>' }) },
  ],
  conditionList: [
    { label: t('17'), value: 1 },
    { label: t('18'), value: 3 }
  ],
  userInfo: {} as any,
  reasonBan: '',
  banType: 1,
  isBanShow: false,
  isControlShow: false,
  rtpList: [] as any[],
  userControl: {
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
    auto_cancel_rtp: 0
  },
  userControlList: [] as any[],
  agentTypeShow:JSON.parse(localStorage.getItem('userInfo') as string).agent_type?true:false
})

const getSearchList = async () => {
  data.userControlList = []
  let query = JSON.parse(JSON.stringify(data.query));
  query = setTimeValue(query)
  query.flag = '1'
  if(query.today_bet){
    query.today_bet = BigTimeWan(query.today_bet)
  }
  if(query.total_bet){
    query.total_bet = BigTimeWan(query.total_bet)
  }
  if(query.today_game_income){
    query.today_game_income = BigTimeWan(query.today_game_income)
  }
  if(query.total_game_income){
    query.total_game_income = BigTimeWan(query.total_game_income)
  }
  let res = await getUserList(query)
  data.list = res.data.list
  data.total = res.data.total
}

const getGameRtp = async (id: number, game_id: number) => {
  let res = await getOperatorTiger({
    agent_id: id, game_id: game_id
  }) as any
  data.rtpList = setArrRtP(res.data.a_slots_rtpshow)
}

// 选择玩家
const playerControl = (val: any) => {
  data.userControlList = val
}
const addControl = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.userControl));
  query.user_ids = []
  data.userControlList.forEach((item: any) => {
    query.user_ids.push(item.agent_id + '_' + item.account3d_id)
  })
  query.switch = 2
  query.rtp = BigTimeHundred(query.rtp)
  query.auto_cancel_rtp = BigTimeHundred(query.auto_cancel_rtp)
  // 选择全部
  const allGame = query.game_ids.find((item: number) => item == -1)
  if (allGame) {
    query.game_ids = []
    indexStore.gameArray.forEach((item: any) => {
      if (item.type == 1) {
        query.game_ids.push(item.value)
      }
    })
  }
  await changeAllControlUser(query)
  ElMessage.success(t('764'))
  onHideDetails()
}, 3000)

// 选择全部游戏
const chooseGameType = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.userControl.game_ids = [-1] as any
  } else {
    data.userControl.game_ids = row
  }
}

const setArrRtP = (str: string) => {
  let arr = [], newArr: any[] = []
  arr = setCommaToArr(str)
  arr.forEach((item: any) => {
    newArr.push(BigHundred(item))
  })
  return newArr
}

// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
}

const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}

// 重置
const onHideDetails = () => {
  data.isControlShow = false
  data.userControlList = []
  data.list = []
  data.total = 0
  data.userControl = {
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
    auto_cancel_rtp: 0
  }
  data.query = {
    page: 1,
    size: 50,
    status: -1,
    search_type: 1,
    search_value: '',
    time_type: 2,
    is_online: -1,
    today_bet: '',
    total_bet: '',
    today_game_income: '',
    total_game_income: '',
    date: getSevenWeek()
  }
}
const emit = defineEmits(['onHideDetails'])
const handleClose = async () => {
  onHideDetails()
  emit('onHideDetails', false)
}
</script>

<style scoped lang="scss"></style>
